package com.nm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nm.commom.Result;
import com.nm.mapper.*;
import com.nm.pojo.*;
import com.nm.service.StopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 停车接口
 */
//@CrossOrigin
@RestController
@RequestMapping(value = "/stop")
public class StopController {

    @Autowired
    private StopService stopService;
    @Autowired
    private StopMapper stopMapper;
    @Autowired
    private StallMapper stallMapper;
    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private ExpendMapper expendMapper;

    /**
     * 根据用户名称查询停车记录
     */
    //
    @RequestMapping(value = "/query")
    public Result query(@RequestParam String nickName) {
        QueryWrapper<Stop> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name", nickName);
        List<Stop> list = stopMapper.selectList(wrapper);
        return Result.success(list);
    }


    /**
     * 查询全部停车记录
     */
    //
    @RequestMapping(value = "/queryall")
    public Result queryallStop() {
        List<Stop> list = stopMapper.selectList(null);
        return Result.success(list);
    }

    /**
     * 根据停车信息ID查询停车信息
     */
    //
    @PostMapping(value = "/queryid")
    public Result queryId(@RequestBody Daom daom) {
        int stopId = daom.getId();
        Stop stop = stopMapper.selectById(stopId);
        return Result.success(stop);
    }

    /**
     * 根据用户名查询停车信息
     */
    //
    @PostMapping(value = "/queryInfo")
    public Result queryId(@RequestParam String nickName) {
        QueryWrapper<Stop> stopQueryWrapper = new QueryWrapper<>();
        stopQueryWrapper.eq("nick_name", nickName);
        List<Stop> stops = stopMapper.selectList(stopQueryWrapper);
        return Result.success(stops);
    }

    /**
     * 停车 金额、stopId不填  stallId要填写
     */
    //
    @PostMapping(value = "/addstop")
    public Result addStop(@RequestBody Stop stop) throws ParseException {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long i = simple.parse(stop.getOverTime()).getTime() - simple.parse(stop.getStartTime()).getTime();
        int hour = (int) (i / 3600000);
        Stall stall1 = stallMapper.selectById(stop.getStallId());
        Integer price = stall1.getPrice();
        QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
        consumerQueryWrapper.eq("nick_name", stop.getNickName());
        Consumer consumer = consumerMapper.selectOne(consumerQueryWrapper);
        if (consumer.getUserMoney() < hour * price) {
            return Result.error(401, "余额不足");
        } else {
            stop.setStopMoney((Integer) hour * price);
            stopMapper.insert(stop);
            stall1.setStallState("使用中");
            stallMapper.updateById(stall1);
            Expend expend = new Expend();
            Date date = new Date();
            String nowTime = simple.format(date);
            expend.setExpendTime(nowTime);
            expend.setTotalMoney(consumer.getUserMoney());
            expend.setUserMoney(hour * price);
            expend.setPtMoney(consumer.getUserMoney() - hour * price);
            expend.setNickName(stop.getNickName());
            List<Stop> stops = stopMapper.selectList(null);
            expend.setStopId(stops.get(stops.size() - 1).getStopId());
            expendMapper.insert(expend);
            QueryWrapper<Income> incomeQueryWrapper = new QueryWrapper<>();
            incomeQueryWrapper.eq("nick_name", stall1.getUserName());
            Income income = incomeMapper.selectOne(incomeQueryWrapper);
            income.setIncomeMoney(income.getIncomeMoney() + (Integer) hour * price);
            incomeMapper.updateById(income);
        }
        return Result.success(stall1.getBleName());
    }

    /**
     * 修改停车记录
     */
    //
    @PostMapping(value = "/update")
    public Result updateStop(@RequestBody Stop stop) {
        stopMapper.updateById(stop);
        return Result.success();
    }

    /**
     * 删除停车记录
     */
    //
    @GetMapping(value = "/delete/{id}")
    public Result deleteStop(@PathVariable int id) {
        stopMapper.deleteById(id);
        return Result.success();
    }

}
